package cn.gbase.jiangsu.data.transfer.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.service.IdEntityService;

import cn.gbase.jiangsu.data.transfer.bean.OperatorsToSupplierAccounts;
import cn.gbase.jiangsu.data.transfer.bean.OrderSupplierView;
import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.common.Constants;

@IocBean(args = { "refer:dao" })
public class SupplierCaiWuService extends IdEntityService<OrderSupplierView> {
	public SupplierCaiWuService() {
		super();
	}

	public SupplierCaiWuService(Dao dao) {
		super(dao);
	}

	//应收款
	public BigDecimal yingskSupplierCount(SupplierUser supplierUser, Cnd cnd) {
		BigDecimal dgxqzjg = new BigDecimal(0.0);
		List<OrderSupplierView> orderSupplierViewList = this.dao().query(OrderSupplierView.class,
				cnd.and("status", "!=", "4").and("supplier_id", "=", supplierUser.getSupplierId()));
		for (OrderSupplierView orderSupplierView : orderSupplierViewList) {
			BigDecimal dczj = new BigDecimal(orderSupplierView.getProductCount()).multiply(
					orderSupplierView.getSellPrice());
			dgxqzjg = dgxqzjg.add(dczj);
		}
		return dgxqzjg;
	}

	//已收款
	public BigDecimal yiskSupplierCount(SupplierUser supplierUser, Cnd cnd) {
		BigDecimal dgxqzjg = new BigDecimal(0.0);
		List<OrderSupplierView> orderSupplierViewList = this.dao().query(
				OrderSupplierView.class,
				cnd.and("status", "!=", "4").and("status", "=", "0")
						.and("supplier_id", "=", supplierUser.getSupplierId()));
		for (OrderSupplierView orderSupplierView : orderSupplierViewList) {
			BigDecimal dczj = new BigDecimal(orderSupplierView.getProductCount()).multiply(
					orderSupplierView.getSellPrice());
			dgxqzjg = dgxqzjg.add(dczj);
		}
		return dgxqzjg;
	}
	

	/**
	 * 取得供应商账目列表.
	 * 
	 * @param pageNumber
	 * @param keyword
	 * @return
	 */
	public QueryResult getSupplierAccountsList(int pageNumber,
			String accountPeriod, int supplierId) {

		if (pageNumber <= 0) {
			pageNumber = 1;
		}

		// 拼接条件
		SqlExpressionGroup group = Cnd.exps("deleteFlag", "=", 0);

		// 账期
		if (accountPeriod != null && accountPeriod.length() > 0) {
			group = group.and("accountPeriod", "like", "%"+accountPeriod+"%");
		}

		// 供应商ID
		if (supplierId > 0) {
			group = group.and("supplierId", "=", supplierId);
		}

		// 最大页数
		Pager pager = this.dao().createPager(pageNumber,
				Constants.LIST_ONE_PAGE_SIZE);

		// 查询记录总数
		int recordCount = this.dao().count(OperatorsToSupplierAccounts.class,
				Cnd.where(group));
		pager.setRecordCount(recordCount);

		// 没数据，直接返回
		if (recordCount == 0) {
			return new QueryResult(
					new ArrayList<OperatorsToSupplierAccounts>(0), pager);
		}

		// 计算页数
		int maxPageNumber = 0;
		if (recordCount % Constants.LIST_ONE_PAGE_SIZE == 0) {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE;
		} else {
			maxPageNumber = recordCount / Constants.LIST_ONE_PAGE_SIZE + 1;
		}
		if (pageNumber > maxPageNumber) {
			pager.setPageNumber(maxPageNumber);
		}

		// 查询
		List<OperatorsToSupplierAccounts> resultList = this.dao().query(
				OperatorsToSupplierAccounts.class,
				Cnd.where(group).desc("createTime"), pager); // 当前页面显示记录

		// 返回
		return new QueryResult(resultList, pager);
	}
}
